<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>util.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_useragent_util.js.html">util.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities used by goog.labs.userAgent tools. These functions
<a name="line17"></a> * should not be used outside of goog.labs.userAgent.*.
<a name="line18"></a> *
<a name="line19"></a> * @visibility {//closure/goog/bin/sizetests:__pkg__}
<a name="line20"></a> * @visibility {//closure/goog/dom:__subpackages__}
<a name="line21"></a> * @visibility {//closure/goog/style:__pkg__}
<a name="line22"></a> * @visibility {//closure/goog/testing:__pkg__}
<a name="line23"></a> * @visibility {//closure/goog/useragent:__subpackages__}
<a name="line24"></a> * @visibility {//testing/puppet/modules:__pkg__} *
<a name="line25"></a> *
<a name="line26"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line27"></a> */
<a name="line28"></a>
<a name="line29"></a>goog.provide(&#39;goog.labs.userAgent.util&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.string&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Gets the native userAgent string from navigator if it exists.
<a name="line36"></a> * If navigator or navigator.userAgent string is missing, returns an empty
<a name="line37"></a> * string.
<a name="line38"></a> * @return {string}
<a name="line39"></a> * @private
<a name="line40"></a> */
<a name="line41"></a>goog.labs.userAgent.util.getNativeUserAgentString_ = function() {
<a name="line42"></a>  var navigator = goog.labs.userAgent.util.getNavigator_();
<a name="line43"></a>  if (navigator) {
<a name="line44"></a>    var userAgent = navigator.userAgent;
<a name="line45"></a>    if (userAgent) {
<a name="line46"></a>      return userAgent;
<a name="line47"></a>    }
<a name="line48"></a>  }
<a name="line49"></a>  return &#39;&#39;;
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Getter for the native navigator.
<a name="line55"></a> * This is a separate function so it can be stubbed out in testing.
<a name="line56"></a> * @return {Navigator}
<a name="line57"></a> * @private
<a name="line58"></a> */
<a name="line59"></a>goog.labs.userAgent.util.getNavigator_ = function() {
<a name="line60"></a>  return goog.global.navigator;
<a name="line61"></a>};
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * A possible override for applications which wish to not check
<a name="line66"></a> * navigator.userAgent but use a specified value for detection instead.
<a name="line67"></a> * @private {string}
<a name="line68"></a> */
<a name="line69"></a>goog.labs.userAgent.util.userAgent_ =
<a name="line70"></a>    goog.labs.userAgent.util.getNativeUserAgentString_();
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Applications may override browser detection on the built in
<a name="line75"></a> * navigator.userAgent object by setting this string. Set to null to use the
<a name="line76"></a> * browser object instead.
<a name="line77"></a> * @param {?string=} opt_userAgent The User-Agent override.
<a name="line78"></a> */
<a name="line79"></a>goog.labs.userAgent.util.setUserAgent = function(opt_userAgent) {
<a name="line80"></a>  goog.labs.userAgent.util.userAgent_ = opt_userAgent ||
<a name="line81"></a>      goog.labs.userAgent.util.getNativeUserAgentString_();
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * @return {string} The user agent string.
<a name="line87"></a> */
<a name="line88"></a>goog.labs.userAgent.util.getUserAgent = function() {
<a name="line89"></a>  return goog.labs.userAgent.util.userAgent_;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * @param {string} str
<a name="line95"></a> * @return {boolean} Whether the user agent contains the given string, ignoring
<a name="line96"></a> *     case.
<a name="line97"></a> */
<a name="line98"></a>goog.labs.userAgent.util.matchUserAgent = function(str) {
<a name="line99"></a>  var userAgent = goog.labs.userAgent.util.getUserAgent();
<a name="line100"></a>  return goog.string.contains(userAgent, str);
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * @param {string} str
<a name="line106"></a> * @return {boolean} Whether the user agent contains the given string.
<a name="line107"></a> */
<a name="line108"></a>goog.labs.userAgent.util.matchUserAgentIgnoreCase = function(str) {
<a name="line109"></a>  var userAgent = goog.labs.userAgent.util.getUserAgent();
<a name="line110"></a>  return goog.string.caseInsensitiveContains(userAgent, str);
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Parses the user agent into tuples for each section.
<a name="line116"></a> * @param {string} userAgent
<a name="line117"></a> * @return {!Array.&lt;!Array.&lt;string&gt;&gt;} Tuples of key, version, and the contents
<a name="line118"></a> *     of the parenthetical.
<a name="line119"></a> */
<a name="line120"></a>goog.labs.userAgent.util.extractVersionTuples = function(userAgent) {
<a name="line121"></a>  // Matches each section of a user agent string.
<a name="line122"></a>  // Example UA:
<a name="line123"></a>  // Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
<a name="line124"></a>  // AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405
<a name="line125"></a>  // This has three version tuples: Mozilla, AppleWebKit, and Mobile.
<a name="line126"></a>
<a name="line127"></a>  var versionRegExp = new RegExp(
<a name="line128"></a>      // Key. Note that a key may have a space.
<a name="line129"></a>      // (i.e. &#39;Mobile Safari&#39; in &#39;Mobile Safari/5.0&#39;)
<a name="line130"></a>      &#39;(\\w[\\w ]+)&#39; +
<a name="line131"></a>
<a name="line132"></a>      &#39;/&#39; +                // slash
<a name="line133"></a>      &#39;([^\\s]+)&#39; +        // version (i.e. &#39;5.0b&#39;)
<a name="line134"></a>      &#39;\\s*&#39; +             // whitespace
<a name="line135"></a>      &#39;(?:\\((.*?)\\))?&#39;,  // parenthetical info. parentheses not matched.
<a name="line136"></a>      &#39;g&#39;);
<a name="line137"></a>
<a name="line138"></a>  var data = [];
<a name="line139"></a>  var match;
<a name="line140"></a>
<a name="line141"></a>  // Iterate and collect the version tuples.  Each iteration will be the
<a name="line142"></a>  // next regex match.
<a name="line143"></a>  while (match = versionRegExp.exec(userAgent)) {
<a name="line144"></a>    data.push([
<a name="line145"></a>      match[1],  // key
<a name="line146"></a>      match[2],  // value
<a name="line147"></a>      // || undefined as this is not undefined in IE7 and IE8
<a name="line148"></a>      match[3] || undefined  // info
<a name="line149"></a>    ]);
<a name="line150"></a>  }
<a name="line151"></a>
<a name="line152"></a>  return data;
<a name="line153"></a>};
</pre>


</body>
</html>
